package com.example.powernodemall.model;

import lombok.Data;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Data
@ToString
public class SecurityUser implements UserDetails {

    private Long userId;
    private String username;
    private String password;
    private Integer status;
    private Long shopId;
    private String logintype;
    private Set<String> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
//        Set<String> roles1 = getRoles123();
//        return roles1.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return logintype+userId;
    }

    @Override
    public boolean isAccountNonExpired() {
        return status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return status == 1;
    }

    @Override
    public boolean isEnabled() {
        return status == 1;
    }
    public Set<String> getRoles123() {
        Set<String> edit_roles = new HashSet<>();
        roles.forEach(role -> {
            if (role.contains(",")){
                String[] strings = role.split(",");
                for (String string : strings){
                    edit_roles.add(string);
                }
            }else {
                edit_roles.add(role);
            }
        });
        return edit_roles;
    }
}
